// component/seckill/seckill.js
var util = require('../../../utils/util')   //引入配置公共请求接口
var configUrl = require('../../../config/config')  //引入配置接口文件
const app=getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data:{
      type: Object,
      // 数据类型
      value:{}
      // 数据值
    }
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  /**
   * 组件的初始数据
   */
  data: {
    imgUrl:getApp().globalData.imgUrl,
    seckillsubColor: '#00AE97',
    seckillBgimg: '' , // 秒杀模块背景图
    seckillData: [], // 秒杀
    countDown: { day : "00", hour: "00", min: "00", sec: "00" },     // 秒杀倒计时
    formatTime: util.newFormatNumber(new Date()),
  },
  attached: function(){
    let that = this
    if (that.properties.data.mmsSeckillData.status == 'ongoing') {
      that.setData({
        endTime: that.properties.data.mmsSeckillData.endTime
      })
      that.countDown()
    }
    if (that.properties.data.mmsSeckillData.status == 'unStart') {
      // let time = util.timedifference(that.data.formatTime, that.properties.data.mmsSeckillData.startTime)
      // console.log('time', time)
      that.setData({
        // countDown: time,
        endTime: that.properties.data.mmsSeckillData.startTime
      })
      that.countDown()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 秒杀
    // getmmsSeckillcurrent() {
    //   let that = this
    //   let addrCode = wx.getStorageSync('addrCode')
    //   let pricePstCode = addrCode.pricePstCode
    //   let formdata = {}
    //   formdata.page = 1
    //   formdata.rows = 6
    //   let mmsSeckillIndexUrl = configUrl.mmsSeckillIndex + '/' + pricePstCode
    //   app.reqFun.requestForm('POST',mmsSeckillIndexUrl,formdata,(res)=>{
    //     if(res.data.success==1 && res.data.data){
    //       let data = res.data.data
    //       if (data.products) {
    //         let records = res.data.data.products
    //         that.setData({
    //           seckillData: records,
    //           endTime: res.data.data.endTime
    //         })
    //         that.countDown()
    //       }
    //     }
    //   })
    // },
    /**
     * 秒杀倒计时
     */
    countDown:function(status){
      let that = this;
      that.setData({
        timer: setInterval(function(){
          var lefttime = parseInt((new Date(that.data.endTime.replace(/-/g,"/")).getTime() - new Date().getTime()));
          if(lefttime <= 0) {
            that.setData({
              countDown:{day:"00",hour:"00",min:"00",sec:"00"}
            })
            clearInterval(that.data.timer);
            return;
          }

          var d = parseInt(lefttime /1000 /3600 /24);  //天数
          var h = parseInt(lefttime / 1000 / 3600 % 24); //小时
          var m = parseInt(lefttime / 1000 / 60 % 60);    //分钟
          var s = parseInt(lefttime / 1000  % 60);        //当前的秒

          d < 10 ? d = "0" + d : d;
          h < 10 ? h = "0" + h : h;
          m < 10 ? m = "0" + m : m;
          s < 10 ? s = "0" + s : s;

          that.setData({
            countDown:{ day: d, hour: h, min: m, sec:s }
          })
        },1000)
      })
    },
    goSeckill() {
      let that = this
      let status = that.properties.data.mmsSeckillData.status
      wx.navigateTo({
        url: '/packagBuy/pages/marketing/seckill/seckill?status='+status
      })
    }
  }
})
